package com.flying.scorer.score.equity;

import com.flying.refdata.model.equity.EquityInfo;
import com.flying.scorer.model.CalcType;
import com.flying.scorer.model.ScorerInfo;
import com.flying.scorer.score.ScoreException;

import java.util.ArrayList;
import java.util.List;

public class CompoundEquityScorer extends AbstractEquityScorer {
    private ScorerInfo info;
    private List<EquityScorer> scorers;

    public CompoundEquityScorer(ScorerInfo info) {
        this(info, null);
    }

    public CompoundEquityScorer(ScorerInfo info, List<EquityScorer> scorers) {
        if (info == null) {
            info = new ScorerInfo();
            info.setName(this.getClass().getSimpleName());
            info.setCalcType(CalcType.ACCUMULATION);
        }
        this.info = info;
        this.scorers = scorers == null ? new ArrayList<>() : scorers;
    }

    public CompoundEquityScorer add(EquityScorer scorer) {
        if (scorer != null) this.scorers.add(scorer);
        return this;
    }

    public CompoundEquityScorer remove(EquityScorer scorer) {
        if (scorer != null) this.scorers.remove(scorer);
        return this;
    }

    @Override
    public ScorerInfo getInfo() {
        return info;
    }

    @Override
    public double score(int date, EquityInfo equityInfo) throws ScoreException {
        if (info.getCalcType() != CalcType.ACCUMULATION)
            throw new ScoreException("Unsupported calculation type: " + info.getCalcType());

        double sum = 0;
        for (EquityScorer scorer : this.scorers) {
            sum += scorer.score(date, equityInfo);
        }
        return sum;
    }
}